package com.hospital.controller;

import com.hospital.entity.FollowUpGroup;
import com.hospital.entity.Level;
import com.hospital.entity.ResponsibleDepartment;
import com.hospital.entity.ResponsibleHospital;
import com.hospital.service.FollowUpGroupService;
import com.hospital.util.JsonUtil;
import com.hospital.util.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 随访组管理 controller层
 */
@Controller
@CrossOrigin
@RequestMapping("FollowUpGroup")
public class FollowUpGroupController {
    @Autowired
    private FollowUpGroupService followUpGroupService;
    @Autowired
    private JsonUtil jsonUtil;

    /**
     * 添加方法
     *
     * @param followUpGroup followUpGroup实体类对象
     * @return 添加状态
     */
    @RequestMapping("Insert")
    @ResponseBody
    public Boolean Insert(FollowUpGroup followUpGroup) {
        //登录时的用户名
        followUpGroup.setCreatedBy("大佬");
        //随访组创建成功时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(new Date());
        followUpGroup.setCreationTime(date);
        followUpGroup.setDeleteState("1");
        followUpGroup.setEnrollmentRate("1%");
        //添加操作
        Integer state = followUpGroupService.Insert(followUpGroup);
        jsonUtil.setRestate(state == 1);
        //返回添加状态
        return jsonUtil.getRestate();
    }

    /**
     * 批量删除随访组方法
     *
     * @param id followUpGroup primary key
     * @return 删除状态
     */
    @RequestMapping("BulkDelete")
    @ResponseBody
    public Boolean BulkDelete(String id) {
        List<Integer> list = new ArrayList<Integer>();
        String[] strs = id.split(",");
        Integer state = 0;
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            state = followUpGroupService.bulkDelete(list);
        }
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 随访组修改方法
     *
     * @param followUpGroup FollowUpGroup实体类对象
     * @return 修改状态
     */
    @RequestMapping("Update")
    @ResponseBody
    public Boolean Update(FollowUpGroup followUpGroup) {
        Integer state = followUpGroupService.Update(followUpGroup);
        jsonUtil.setRestate(state == 1);
        return jsonUtil.getRestate();
    }

    /**
     * 分页查所有随访组
     *
     * @param page    当前页
     * @param pageNum 每页显示条数
     * @return 集合中的FollowUpGroup实体类对象
     */
    @RequestMapping("SelectAll")
    @ResponseBody
    public List<Object> SelectAll(Integer page, Integer pageNum) {
        //查所有数量方法
        Integer count = followUpGroupService.selectAllCount();
        //分页方法
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        //查所有方法
        List<FollowUpGroup> followUpGroups = followUpGroupService.selectAll(page, pageNum);
        List<Object> list = new ArrayList<>();
        //所有数据
        list.add(followUpGroups);
        //分页数据
        list.add(paging);
        jsonUtil.setResults(list);
        return jsonUtil.getResults();
    }

    /**
     * 分页模糊查询
     *
     * @param followUpGroup FollowUpGroup实体类对象
     * @param page          当前页数
     * @param pageNum       每页显示页数
     * @return 集合中的FollowUpGroup实体类对象
     */
    @RequestMapping("SelectLikeCondition")
    @ResponseBody
    public List<Object> SelectLikeCondition(FollowUpGroup followUpGroup, Integer page, Integer pageNum) {
        //负责人模糊查询
        String personInCharge = followUpGroup.getPersonInCharge();
        if (personInCharge!=null){
            followUpGroup.setPersonInCharge("%" + personInCharge + "%");
        }
        //模糊查询数量
        Integer count = followUpGroupService.SelectLikeConditionCount(followUpGroup);
        //分页方法
        Map<String, Integer> paging = PagingUtil.paging(page, pageNum, count);
        //当前页
        Integer pages = paging.get("page");
        //每页显示条数
        Integer pageNums = paging.get("pageNum");
        //分页模糊查询
        List<FollowUpGroup> followUpGroups = followUpGroupService.SelectLikeCondition(followUpGroup, pages, pageNums);
        //返回集合
        List<Object> list = new ArrayList<>();
        //所有数据
        list.add(followUpGroups);
        //分页数据
        list.add(paging);
        jsonUtil.setResults(list);
        return jsonUtil.getResults();
    }

    /**
     * 根据FollowUpGroup 主键查询随访组
     *
     * @param id FollowUpGroup primary key
     * @return FollowUpGroup实体类对象
     */
    @RequestMapping("SelectByFollowUpGroupId")
    @ResponseBody
    public FollowUpGroup SelectByFollowUpGroupId(Integer id) {
        FollowUpGroup followUpGroup = followUpGroupService.SelectByFollowUpGroupId(id);
        return followUpGroup;
    }

    /**
     * 查询责任医院
     *
     * @return 集合中的ResponsibleHospital实体类对象
     */
    @RequestMapping("SelectByResponsibleHospital")
    @ResponseBody
    public List<ResponsibleHospital> SelectByResponsibleHospital() {
        List<ResponsibleHospital> responsibleHospitals = followUpGroupService.SelectByResponsibleHospital();
        return responsibleHospitals;
    }

    /**
     * 查询责任科室
     *
     * @return 集合中的ResponsibleDepartment实体类对象
     */
    @RequestMapping("SelectByResponsibleDepartment")
    @ResponseBody
    public List<ResponsibleDepartment> SelectByResponsibleDepartment() {
        List<ResponsibleDepartment> responsibleDepartments = followUpGroupService.SelectByResponsibleDepartment();
        return responsibleDepartments;
    }

    /**
     * 查询所有级别
     *
     * @return 集合中的Level实体类对象
     */
    @RequestMapping("SelectByLevel")
    @ResponseBody
    public List<Level> SelectByLevel() {
        List<Level> levels = followUpGroupService.SelectByLevel();
        return levels;
    }
}
